<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_date".
 *
 * @property int $id
 * @property int $tid
 * @property string $date
 * @property int $inventory
 * @property string $ori_price
 * @property string $ruling_price
 * @property string $update_auth
 * @property string $state
 * @property string $create_time
 */
class TicketDate extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_date';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['tid', 'date', 'inventory', 'ori_price', 'ruling_price'], 'required'],
            [['tid', 'inventory'], 'integer'],
            [['ori_price', 'ruling_price'], 'number'],
            [['state'], 'string'],
            [['create_time'], 'safe'],
            [['date'], 'string', 'max' => 10],
            [['update_auth'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tid' => 'Tid',
            'date' => 'Date',
            'inventory' => 'Inventory',
            'ori_price' => 'Ori Price',
            'ruling_price' => 'Ruling Price',
            'update_auth' => 'Update Auth',
            'state' => 'State',
            'create_time' => 'Create Time',
        ];
    }


    public static function ticketDate($tid,$ticketInfo)
    {
        $nowData = date("Y-m-d");
        // 当前时间是否超过限购时间
        $nowHour = date("H:i");
        // 提前的预约天数
        $advance_num = TicketQuotaRule::find()->where('tid = :id',[':id' => $tid])->select(['advance_num'])->asArray()->one()['advance_num'];
        if (isset($ticketInfo['otherTime']) and !empty($ticketInfo['otherTime'])){
            $reserveTime = $ticketInfo['otherTime'];
            sort($reserveTime);
            $lastTime = array_pop($reserveTime);
            $endTime = explode('-',$lastTime)[1];
        }else{
            $endTime = '24:00';
        }
        $ticketDateInfo = self::find()->where('tid = :id',[':id' => $tid])->andWhere(['>','inventory',0])->andFilterWhere(['>=','date',$nowData])->select(['id','tid','date','ori_price','ruling_price','inventory'])->orderBy(['date' => SORT_ASC])->asArray()->all();
        if (!empty($ticketDateInfo)){
            foreach ($ticketDateInfo as $key => $val){
                if ($nowData == $val['date']){  // 当前时间超过每天最晚预约时间，删除当天日期数据
                    if ($nowHour > $endTime){
                        unset($val);
                    }
                    $today = [
                        'price' => $val['ruling_price'],
                        'inventory' => $val['inventory']
                    ];
                }
                if (!empty($advance_num)){ // 根据提前预约天数， 删除对应的日期
                    for ($i = 0;$i<$advance_num;$i++){
                        if (date("Y-m-d",strtotime("+$i day")) == $val['date']){
                            unset($val);
                        }
                    }
                }

                if (isset($ticketInfo['now_ticket_price']) and !empty($ticketInfo['now_ticket_price']) and $ticketInfo['now_ticket_price'] > 0 ){
                    if ($nowData == $val['date']){
                        unset($val);
                    }
                }
                if (!empty($val)){
                    $ticketDateList[] = $val;
                }

            }
        }
        if (isset($ticketInfo['now_ticket_price']) and !empty($ticketInfo['now_ticket_price'])  and $ticketInfo['now_ticket_price'] > 0 ){
            $todayDate = [
                'id' => 0,
                'tid' => $tid,
                'date' => $nowData,
                'ruling_price' => $ticketInfo['now_ticket_price'],
                'inventory' => 999,
            ];
            array_unshift($ticketDateList,$todayDate);
            $today = [
                'price' => $ticketInfo['now_ticket_price'],
                'inventory' => 999,
            ];
        }

        return [$ticketDateList,$today];
    }



    /*** 根据指定日期获取门票价格
     * @param $tid
     * @param $date
     */
    public function getPriceByDate($tid,$date,$ticketInfo)
    {
        if ($date == date("Y-m-d")){
            if  (isset($ticketInfo['now_ticket_price']) and !empty($ticketInfo['now_ticket_price'])){
                return $ticketInfo['now_ticket_price'];
            }
        }
        $ticketDateInfo = self::find()->where('tid = :id and date = :date',[':id' => $tid,':date' => $date])->select(['ruling_price','inventory'])->asArray()->one();
        if (!empty($ticketDateInfo['ruling_price']) and $ticketDateInfo['inventory'] != 0){
            return $ticketDateInfo['ruling_price'];
        }
        return false;

    }
}
